﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>提交调休</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <link href="../../../layuiadmin/style/mystyle.css" rel="stylesheet" />
    <script src="../../../layuiadmin/artdialog/artDialog.js?skins=chrome"></script>
    <script src="../../../layuiadmin/lib/jquery.min.js"></script>
    <script src="../../../layuiadmin/lib/api.js"></script>
    <script src="../../../layuiadmin/lib/common.js"></script>
    <!--编辑器-->
    <link href="../../../layuiadmin/kindeditor/themes/default/default.css" rel="stylesheet" />
    <script src="../../../layuiadmin/kindeditor/kindeditor-all.js"></script>
    <script src="../../../layuiadmin/kindeditor/lang/zh-CN.js"></script>
    <script src="../../../layuiadmin/layui/layui.js"></script>
    <script src="../../../layuiadmin/lib/system.js"></script>
    <script>
        //检查服务器是否已登录，没有登录则跳转至登录页。
        sys_islogin();
    </script>
</head>
<body>

    <div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
        <div style="margin-bottom: 45px;">
            <input type="hidden" id="ID" name="ID" value="" />
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">开始时间</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" id="STARTTIME" lay-verify="required" name="STARTTIME" placeholder="开始时间">
                    </div>

                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">结束时间</label>

                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" id="ENDTIME" lay-verify="required" name="ENDTIME" placeholder="结束时间">
                    </div>

                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">调休时长</label>
                <div class="layui-input-inline">
                    <input type="text" name="LEAVETIMES" id="LEAVETIMES" lay-verify="required" placeholder="0" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div>
                    <textarea name="DETAIL" id="DETAIL" style=" height: 200px;width:80%;" autocomplete="off" class="layui-textarea"></textarea>
                </div>

            </div>
        </div>


        <div class="layui-form-item layui-layout-admin" style="text-align:center;">
            <div class="layui-footer-new" style="background-color:#fff;">
                <button class="layui-btn" lay-submit lay-filter="save" id="save">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="close">取消</button>
            </div>
        </div>
    </div>


    <script>

        layui.config({
            base: '../../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'form', 'laydate', 'upload'], function () {
            var $ = layui.$
                , form = layui.form, upload = layui.upload;

            //监听提交
            form.on('submit(save)', function (data) {
                var field = data.field; //获取提交的字段
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.ajax({
                    url: "/LeaveMerber/AddAndUpdateLeave",    //请求的url地址
                    dataType: "json",   //返回格式为json
                    async: true,//请求是否异步，默认为异步，这也是ajax重要特性
                    data: { arg: JSON.stringify(field) },    //参数值
                    type: "POST",   //请求方式
                    beforeSend: function () {
                        //请求前的处理
                    },
                    success: function (data) {
                        console.log(data);
                        //请求成功时处理
                        layer.msg(data.message);
                        if (data.flag > 0) {
                            window.parent.location.reload();//刷新父页面
                            parent.layer.close(index); //再执行关闭
                        }

                    },
                    error: function (res) {
                        console.log(res);
                        layer.msg("添加失败，请联系技术人员");
                    }
                });

            });

            var laydate = layui.laydate;
            //开始日期
            var insStart = laydate.render({
                elem: '#STARTTIME'
                //, min: 0
                , type: 'datetime'

                , done: function (value, date) {
                    //更新结束日期的最小日期
                    insEnd.config.min = lay.extend({}, date, {
                        month: date.month - 1
                    });

                    //自动弹出结束日期的选择器
                    insEnd.config.elem[0].focus();

                    //计算加班时长
                    var endtime = $("#ENDTIME").val();
                    if (endtime && value) {
                        if (value > endtime) {
                            return layer.msg("开始时间不能大于结束时间");
                        }
                        GetLeaveTimes(value, endtime);
                    }
                }
            });

            //结束日期
            var insEnd = laydate.render({
                elem: '#ENDTIME'
                // , min: 0
                , type: 'datetime'
                , done: function (value, date) {
                    //更新开始日期的最大日期
                    insStart.config.max = lay.extend({}, date, {
                        month: date.month - 1
                    });
                    //计算加班时长
                    var starttime = $("#STARTTIME").val();
                    
                    if (starttime && value) {
                        if (starttime > value) {
                            return layer.msg("开始时间不能大于结束时间");
                        }
                        GetLeaveTimes(starttime, value);
                    }
                }
                , change: function (value, date, endDate) {


                }
            });

            
        })


        //接收父页面的数据
        function child_fill_data(data) {
            console.log(data);
            for (let key in data) {
                $("#" + key.toUpperCase()).val(data[key]);             
            }
        };


        //关闭弹窗
        $("#close").on("click", function () {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        });

        function GetLeaveTimes(start,end) {
            $.ajax({
                url: "/LeaveMerber/CalculateLeave",    //请求的url地址
                dataType: "json",   //返回格式为json
                async: false,//请求是否异步，默认为异步，这也是ajax重要特性
                data: { startTime: start, endTime:end},    //参数值
                type: "POST",   //请求方式
                beforeSend: function () {
                    //请求前的处理
                },
                success: function (data) {
                    //请求成功时处理
                    $("#LEAVETIMES").val(data);
                },
                error: function (res) {
                    console.log(res);
                    layer.msg("添加失败，请联系技术人员");
                }
            });
        }
    </script>
</body>
</html>